<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Api_Model_MessageMapper {
	protected $_db_table;

	public function __construct() {
		$this->_db_table = new Api_Model_DbTable_Message();
	}

	public function getAllMessage($user_id) {
		$select = $this->_db_table->select()->from('messages', array('*'))->where('receiver = ?', $user_id)
		->orWhere('sender = ?', $user_id);

		$result = $this->_db_table->fetchAll($select);
		$db_user = new Api_Model_DbTable_User();

		if(count($result) == 0) {
			throw new Exception(json_encode(array('meta' => 'Not found')),404);
		} else {
			foreach ($result as $row) {
				// $row should be a Zend_Db_Table_Row object
				// cast to array
				$select_sender = $db_user->select()->from('users', array('*'))->where('user_id = ?', $row->sender);
				$result_sender = $this->_db_table->fetchAll($select_sender);
				$row_sender = $result_sender->current();
				$select_receiver = $db_user->select()->from('users', array('*'))->where('user_id = ?', $row->receiver);
				$result_receiver = $this->_db_table->fetchAll($select_receiver);
				$row_receiver = $result_receiver->current();
				$rowArray = $row->toArray();

				$rowArray['sender'] = array('user_id' => $row_sender->user_id,
                                              'username' => $row_sender->username,
                                              'fullname' => $row_sender->fullname,
                                              'profile_picture' => $row_sender->profile_picture
				);

				$rowArray['receiver'] = array('user_id' => $row_receiver->user_id,
                                              'username' => $row_receiver->username,
                                              'fullname' => $row_receiver->fullname,
                                              'profile_picture' => $row_receiver->profile_picture
				);

				$json[] = array('message' => $rowArray);

			}
			return $json;
		}
	}

	public function getconversationMessage($sender, $receiver) {
		 
		$select = $this->_db_table->select()->from('messages', array('*'))
		->where('receiver = '.$receiver.' AND sender = '.$sender)
		->orWhere('receiver = '.$sender.' AND sender = '.$receiver)
		->order('created_time');

		$result = $this->_db_table->fetchAll($select);
		$db_user = new Api_Model_DbTable_User();

		if(count($result) == 0) {
			throw new Exception(json_encode(array('meta' => 'Not found')),404);
		} else {
			foreach ($result as $row) {
				// $row should be a Zend_Db_Table_Row object
				// cast to array
				$select_sender = $db_user->select()->from('users', array('*'))->where('user_id = ?', $row->sender);
				$result_sender = $this->_db_table->fetchAll($select_sender);
				$row_sender = $result_sender->current();
				$select_receiver = $db_user->select()->from('users', array('*'))->where('user_id = ?', $row->receiver);
				$result_receiver = $this->_db_table->fetchAll($select_receiver);
				$row_receiver = $result_receiver->current();
				$rowArray = $row->toArray();

				$rowArray['sender'] = array('user_id' => $row_sender->user_id,
                                              'username' => $row_sender->username,
                                              'fullname' => $row_sender->fullname,
                                              'profile_picture' => $row_sender->profile_picture
				);

				$rowArray['receiver'] = array('user_id' => $row_receiver->user_id,
                                              'username' => $row_receiver->username,
                                              'fullname' => $row_receiver->fullname,
                                              'profile_picture' => $row_receiver->profile_picture
				);

				$json[] = array('message' => $rowArray);

			}
			return $json;
		}
	}

	public function sendMessage($data){

		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$message = array('receiver' => $data['receiver'],
                        'sender' => $data['sender'],
                        'content' => $data['content'],
                        'status' => 'unread');
		$affectedRow = $this->_db_table->insert($message);

		if ($affectedRow){
			$message_id = $this->_db_table->getAdapter()->lastInsertId();

			$select = $this->_db_table->select()->from('messages', '*')->where('message_id = ?', $message_id);
			$result = $this->_db_table->fetchAll($select);
			$row = $result->current();
			$arrayRow = $row->toArray();

			return $arrayRow;

		} else {
			throw new Exception('Internal Server Error' ,500);
		}
	}

	public function readMessage($message_id){
		$where = array('message_id = "'.$message_id.'"');
		$this->_db_table->update(array('status' => 'read'), $where);
	}

}
